package com.hzcx.app.simplechat.ui.publicui;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseDialog;
import com.hzcx.app.simplechat.ui.publicui.adapter.PublicDialogAdapter;
import com.hzcx.app.simplechat.ui.publicui.interfaces.OnPublicMenuOnClickListener;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;

import java.util.List;

import butterknife.BindView;

/**
 * 通用的底部圆角弹窗
 */
public class PublicRoundDialog extends BaseDialog {
    @BindView(R.id.rv_menu)
    RecyclerView rvMenu;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.cons_menu)
    ConstraintLayout consMenu;

    private List<String> list;
    private PublicDialogAdapter dialogAdapter;
    private OnPublicMenuOnClickListener listener;
    private String title;

    public PublicRoundDialog(@NonNull Context context, List<String> list, OnPublicMenuOnClickListener listener) {
        this(context, null, list, listener);
    }

    public PublicRoundDialog(@NonNull Context context, String title, List<String> list, OnPublicMenuOnClickListener listener) {
        super(context);
        this.list = list;
        this.listener = listener;
        this.title = title;
    }

    @Override
    public int getLayout() {
        return R.layout.dialog_public_round;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {

        // 显示在最底部
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        tvCancel.setOnClickListener(view -> dismiss());

        dialogAdapter = new PublicDialogAdapter(R.layout.rv_item_public_menu, list);
        rvMenu.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        rvMenu.setAdapter(dialogAdapter);

        if (EmptyUtils.isNotEmpty(title)) {
            tvTitle.setVisibility(View.VISIBLE);
            viewLine.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
    }

    @Override
    public void initData() {

        dialogAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (listener != null) {
                listener.menuOnClick(position, list.get(position));
            }
            dismiss();
        });

    }

}
